package com.zretc.controller;

import com.config.ServiceException;
import com.util.CodeMsg;
import com.util.Result;
import com.zretc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Filename:    UploadResource
 * Description: 影像上传控制器
 * Copyright:   Copyright (c) 2018-2019 All Rights Reserved.
 * Company:     chinasofti.com Inc.
 *
 * @author: mazheng
 * @version: 1.0
 * Create at:   2022--8-23
 * <p>
 * Modification History:
 * Date         Author      Version     Description        tag
 * ------------------------------------------------------------------
 * 2022--8-23      mazheng      1.0         1.0 Version        new
 */
@RestController
public class UploadResource {
    @Autowired
    private Environment environment;

//    /**
//     * 单文件接收二进制方式
//     * @param file   file文件
//     * @param sid    身法证号
//     * @param req   请求对象
//     * @return
//     * @throws ServiceException 业务异常
//     */
//    @PostMapping("/upload_sx")
//    public Result upload_sx(@RequestPart("file") Part  file,@RequestPart String sid, HttpServletRequest req) throws ServiceException {
//        String sufix = file.getSubmittedFileName().substring(file.getSubmittedFileName().lastIndexOf("."));
//        String path = environment.getProperty("fileupload.path");
//        String filename = sid + "@" + UUID.randomUUID().toString() + sufix;
//        try {
//            FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(new File(path, filename)));
//        } catch (IOException e) {
//            e.printStackTrace();
//            throw new ServiceException("500401", e);
//        }
//        return Result.success(filename);
//    }
//
//    @PostMapping("/upload_sx_all")
//    public Result upload_sx(@RequestParam(value = "imgs", required = false) List<String> imgs, String sid) throws ServiceException {
//
//        System.out.println(imgs.size());
//
//        // 返回文件名数组
//        return Result.success("xx");
//    }
//
//    /**
//     * 根据文件名字删除
//     * @param filename
//     * @param req
//     * @return
//     * @throws ServiceException
//     */
//    @GetMapping("/upload_del")
//    public Result upload_sx(@RequestParam String filename, HttpServletRequest req) throws ServiceException {
//
//
//        String path = environment.getProperty("fileupload.path");
//        File f = new File(path, filename);
//        boolean flg = f.delete();
//
//        return flg ? Result.success("success"): Result.success(CodeMsg.messageprop.get("5004011"));
//    }
}
